<#assign treeid=auth.getuuid()>
<#assign treedetailid=auth.getuuid()>
<#assign nameid=auth.getuuid()>
<#assign idid=auth.getuuid()>
<#assign noid=auth.getuuid()>

<script type="text/javascript">
 $(function(){ 
	var $panel = $.CurrentNavtab;
	var $t     = $('#${treeid}');
	var op     = $t.data();
	//添加删除按钮INIT
	var add    = op.addHoverDom ? null : 'edit';
	var remove = op.removeHoverDom ? null : 'edit';
	var onremove = op.onRemove ? '' : 'M_NodeRemove';
	$t.data('addHoverDom', add).data('removeHoverDom', remove).data("onRemove",onremove);
	//拖动INIT
	var edit   = op.editEnable ? true : true,
        bDrag  = op.beforeDrag ? '' : 'M_BeforeNodeDrag',
        bDrop  = op.beforeDrop ? '' : 'M_BeforeNodeDrop',
        onDrop = op.onDrop ? '' : 'M_NodeDrop';
            
    $t.data('editEnable', edit).data('beforeDrag', bDrag).data('beforeDrop', bDrop).data('onDrop', onDrop);
}); 

//单击事件
function ZtreeClick(event, treeId, treeNode) {
    var $detail = $('#${treedetailid}');
    if ($detail.attr('tid') == treeNode.tId){
    	return;
    }
	if (treeNode.name) {
		$('#${nameid}').val(treeNode.name);
	}else{
		$('#${nameid}').val('');
	}
	if (treeNode.tsid) {
		$('#${idid}').val(treeNode.tsid);
	} else {
		$('#${idid}').val('');
	}
	if (treeNode.tsno) {
		if (isNaN(treeNode.tsno)){
			$('#${noid}').val(treeNode.tsno);
		}else{
			var no = treeNode.tsno;
			var nos = String(no);
			if(nos.length < 4){
				for(var i=4-nos.length;i>0;i--){
					nos="0"+nos;
				}
			}
			$('#${noid}').val(nos);
		}
	} else {
		$('#${noid}').val('');
	}
	
	$detail.attr('tid', treeNode.tId);
    $detail.show();
}
//保存属性
function M_Ts_Menu() {
	var zTree  = $.fn.zTree.getZTreeObj("${treeid}");
	var tsname   = $('#${nameid}').val();
	var tsno    = $('#${noid}').val();
	var tsid	 = $('#${idid}').val();


	if ($.trim(tsname).length == 0) {
		alertMsg.error('名称不能为空！');
		return;
	}
	var upNode = zTree.getSelectedNodes()[0];
	
	if (!upNode) {
		alertMsg.error('未选中任何对象！');
        return;
	}
	upNode.name   = tsname;
	upNode.tsname = tsname;
	upNode.tsno   = tsno;
	upNode.tsid 	= tsid;
	zTree.updateNode(upNode);
}
//监听拖拽事件
function M_BeforeNodeDrag(treeId, treeNodes) {
    return true
}
function M_BeforeNodeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
    /*禁止插入层级为2的节点*/
    if (moveType == 'inner' && targetNode.level == 8) {
    	$("#${treeid}").alertmsg('info', '拖拽失败。原因：组织架构只能有8层！');
        return false
    }
    /*禁止插入剩余层级不足的子节点*/
    if (moveType == 'inner' && treeNodes[0].isParent) {
        var molevel = 8 - targetNode.level //剩余层级
        var maxlevel = 1
        var zTree = $.fn.zTree.getZTreeObj("${treeid}")
        var nodes = zTree.transformToArray(treeNodes)
        var level = nodes[0].level
        
        for (var i = 1; i < nodes.length; i++) {
            if (nodes[i].level == (level + 1)) {
                maxlevel++
                level++
            }
        }
        if (maxlevel > molevel) {
        	$("#${treeid}").alertmsg('info', '拖拽失败。原因：组织架构只能有8层！');
            return false
        }
    }
    return true
}
//拖拽结束事件
function M_NodeDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
    
}
//删除结束事件
var menudeleteids = [];
function M_NodeRemove(event, treeId, treeNode) {
	if(treeNode.tsid!=null&&treeNode.tsid!=""){
		menudeleteids.push(treeNode.tsid);
	}
}

//保存菜单
function savemenu(){
	var zTree = $.fn.zTree.getZTreeObj("${treeid}")
    var menus = zTree.getNodes();
	
	$("#${treeid}").bjuiajax('doAjax', {url:"${rootpath}/System/org_save",confirmMsg:"是否确定需要保存？",data:{'menus':JSON.stringify(menus[0]),"deleteids":JSON.stringify(menudeleteids)}});
	menudeleteids = [];
}
</script>
<div class="bjui-pageContent">
        <div style="float:left; margin:5px 5px 0; width:400px;">
            <fieldset>
                <legend>组织架构</legend>
                <#if auth.getauth(userid,"/System/org_save")>
                <button class="btn btn-green" onclick="savemenu();">保存更新</button>
                </#if>
                <div class="alert alert-info form-inline"><i class="fa fa-info-circle"></i> 可拖拽菜单节点</div>
                <div class="clearfix">
                    <div style="float:left; width:400px; height:625px; overflow:auto;">
                        <ul id="${treeid}" class="ztree" data-toggle="ztree" data-max-add-level="8" data-expand-level="2" data-on-click="ZtreeClick">
                            <!-- <li data-id="1" data-pid="0">表单元素</li>
                            <li data-id="10" data-pid="1" data-url="form-button.html" data-tabid="form-button">按钮</li> -->
                            <#list menulist as item>
                            	<#if item?exists>
                            		<li data-id="${item.id!}" data-pid="${item.pid!}" data-tsid="${item.id!}" data-tspid="${item.pid!}" 
                            		data-tsname="${item.name!}" data-tsno="${item.no!}" data-faicon="<#if (item.no)!=''>user<#else>building</#if>">${item.name}</li>
                            	</#if>
                            </#list>
                        </ul>
                    </div>
                </div>
            </fieldset>
        </div>
        <!-- 明细编辑START -->
        <div id="${treedetailid}" style="display:none; margin-left:430px; margin-top:5px; width:600px; height:95px;">
        	<fieldset>
            <legend>详细信息</legend>
            	<div class="form-group">
                    <label for="${idid}" class="control-label x85">id：</label>
                    <input type="text" class="form-control" name="m.tsid" id="${idid}" size="15" placeholder="" readonly />
                </div>
                <div class="form-group">
                    <label for="${nameid}" class="control-label x85">名称：</label>
                    <input type="text" class="form-control validate[required] required" name="m.tsname" id="${nameid}" size="15" placeholder="" />
                	<label for="${noid}" class="control-label x85">工号：</label>
                    <input type="text" class="form-control" name="m.tsno" id="${noid}" size="15" placeholder="" />
                    <#if auth.getauth(userid,"/System/org_save")>
                    <button class="btn btn-green" onclick="M_Ts_Menu();">确认修改</button>
                    </#if>
                </div>
            </fieldset>
        </div>
        <!-- 明细编辑END -->
</div>